using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MissionBeacon : MonoBehaviour
{
	[SerializeField]
	private List<GameObject> missions = new List<GameObject>();

	[SerializeField]
	private string cMissionName;

	[SerializeField]
	private NJGMapItem icon;

	public bool visible = true;

	public Animator tonyAnimator;

	private bool oneMissionCompleate;

	private bool onTrigger;

	public void ResetMission()
	{
		cMissionName = missions[0].name;
		BCWStorage.Set("MissionBeacon_" + base.gameObject.name, cMissionName);
		BCWStorage.Set("Missions_Ended" + DistrictDetector.instance.GetCurrentDistrictName(base.transform.position), false);
		BCWStorage.Set("MissionBeacon_Started", string.Empty);
	}

	public void Awake()
	{
		ShowBeacon(false);
	}

	private void Start()
	{
		cMissionName = BCWStorage.Get<string>("MissionBeacon_" + base.gameObject.name);
		if (cMissionName == string.Empty || (missions.Find((GameObject x) => x != null && x.name == cMissionName) == null && cMissionName != "NoMission"))
		{
			ResetMission();
		}
		ManagerMissions.completeMission += OnMissionCompleate;
		ManagerMissions.startMission += OnMissionStart;
		StartCoroutine(UpdateBeacon());
	}

	public IEnumerator UpdateBeacon()
	{
		while (true)
		{
			UpdateBeaconVisible();
			yield return new WaitForSeconds(Random.Range(1, 4));
		}
	}

	private void UpdateBeaconVisible()
	{
		bool flag = false;
		if (settings.offlineMode)
		{
			flag = true;
		}
		if (ManagerMissions.instance != null)
		{
			ShowBeacon(ManagerMissions.instance.startedMission.Find((MissionBase x) => x.name == cMissionName) == null && cMissionName != "NoMission" && flag);
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (!visible || onTrigger)
		{
			return;
		}
		PlayerBehavior componentInParent = other.GetComponentInParent<PlayerBehavior>();
		if (componentInParent == GameController.instance.playerScript && ManagerMissions.instance.startedMission.Find((MissionBase x) => x.name == cMissionName) == null)
		{
			ManagerMissions.instance.ShowNewMissionWindow(missions.Find((GameObject x) => x != null && x.name == cMissionName).GetComponent<MissionBase>());
			onTrigger = true;
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (visible)
		{
			PlayerBehavior componentInParent = other.GetComponentInParent<PlayerBehavior>();
			if (componentInParent == GameController.instance.playerScript)
			{
				ManagerMissions.instance.HideMissionWindow();
				onTrigger = false;
			}
		}
	}

	public void ShowBeacon(bool val)
	{
		if (visible != val)
		{
			icon.enabled = val;
			visible = val;
			Renderer[] componentsInChildren = GetComponentsInChildren<Renderer>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].enabled = visible;
			}
			Collider[] componentsInChildren2 = GetComponentsInChildren<Collider>();
			for (int j = 0; j < componentsInChildren2.Length; j++)
			{
				componentsInChildren2[j].enabled = visible;
			}
		}
	}

	private void OnDestroy()
	{
		ManagerMissions.completeMission -= OnMissionCompleate;
		ManagerMissions.startMission -= OnMissionStart;
	}

	public void OnMissionStart()
	{
		if (cMissionName == ManagerMissions.currMission.gameObject.name)
		{
			ManagerMissions.instance.HideMissionWindow();
			onTrigger = false;
			BCWStorage.Set("MissionBeacon_Started", base.gameObject.name);
			BCWStorage.Set("MissionBeacon_" + base.gameObject.name, cMissionName);
			UpdateBeaconVisible();
		}
	}

	public void OnMissionCompleate()
	{
		if (cMissionName == ManagerMissions.currMission.gameObject.name)
		{
			oneMissionCompleate = true;
			int num = missions.IndexOf(missions.Find((GameObject x) => x.name == cMissionName));
			num++;
			if (num < missions.Count)
			{
				cMissionName = missions[num].name;
			}
			else
			{
				BCWStorage.Set("Missions_Ended" + DistrictDetector.instance.GetCurrentDistrictName(base.transform.position), true);
				cMissionName = "NoMission";
			}
			BCWStorage.Set("MissionBeacon_" + base.gameObject.name, cMissionName);
			BCWStorage.Set("MissionBeacon_Started", string.Empty);
		}
	}

	public void StartMission(string missionName, bool reset = false)
	{
		ManagerMissions.instance.StartMissionWithId(missionName, reset);
	}
}
